/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeronef.model;

import com.jme3.app.state.AppStateManager;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hagan
 */
public class Ship extends AeronefNode {
    
    private String name;
    private Float forwardThrust = 0f;
    private Float turnThrust = 0f;
    
    private Float orderSpeed = 0f;
    private Float orderTurn = 0f;
    private Float orderRoll = 0f;
    private Float orderClimb = 0f;
    private Float orderTarget = 0f;
    
    private Node targeterNode;
    
    private List<Battery> batteries = new ArrayList<Battery>();
    
    private AppStateManager stateManager;
    
    public Ship(AppStateManager stateManager ) {
        super(stateManager);
    }
    
    public void act() {
        
        RigidBodyControl shipPhysics = getControl(RigidBodyControl.class);
        performSpeed();
        performTurn();
        performClimb();
        shipPhysics.applyCentralForce(new Vector3f(0,new Float(34.335), 0));
    }
        
    private void performSpeed() {
        RigidBodyControl shipPhysics = getControl(RigidBodyControl.class);
        Quaternion quat = shipPhysics.getPhysicsRotation(Quaternion.IDENTITY);
        Vector3f newVector = quat.mult(new Vector3f(0,0,orderSpeed));
        shipPhysics.applyCentralForce(newVector);
    }
    
    
    private void performTurn() {
        RigidBodyControl shipPhysics = getControl(RigidBodyControl.class);
        shipPhysics.applyTorque(new Vector3f(0,orderTurn,0));
    }
    
    private void performClimb() {
        RigidBodyControl shipPhysics = getControl(RigidBodyControl.class);
        Vector3f thrustVector = new Vector3f(0,orderClimb,0);
        shipPhysics.applyCentralForce(thrustVector);
    }
    
    public void speed(Float speed) {
        
        orderSpeed = orderSpeed + speed;
        if (orderSpeed > forwardThrust) {
            orderSpeed = forwardThrust;
        } else if (orderSpeed < -forwardThrust/2) {
            orderSpeed = -forwardThrust/2;
        }
    }
    public void turn(Float turn) {
        orderTurn = orderTurn + turn;
        if (orderTurn > new Float(2)) {
            orderTurn = new Float(2);
        } else if (orderTurn < new Float(-2)) {
            orderTurn = new Float(-2);
        }
    }
    public void roll(Float roll) {
        orderRoll = orderRoll + roll;
        if (orderRoll > new Float(2)) {
            orderRoll = new Float(2);
        } else if (orderRoll < new Float(-2)) {
            orderRoll = new Float(-2);
        }
    }
    public void climb(Float climb) {
        orderClimb = orderClimb + climb;
        if (orderClimb > turnThrust) {
            orderClimb = turnThrust;
        } else if (orderClimb < -turnThrust) {
            orderClimb = -turnThrust;
        }
    }
    
    public void trainClockwise(float tpf) {
        
        orderTarget -= tpf*50;
        if (orderTarget < -180) { 
            float leftover = orderTarget % 180;
            orderTarget = 180 - leftover;
        }
        targeterNode.setLocalRotation(new Quaternion().fromAngles(0, orderTarget*FastMath.DEG_TO_RAD, 0)); 
    }
    public void trainAnticlockwise(float tpf) {
        
        orderTarget += tpf*50;
        if (orderTarget > 180) { 
            float leftover = orderTarget % 180;
            orderTarget = -180 + leftover;
        }
        targeterNode.setLocalRotation(new Quaternion().fromAngles(0, orderTarget*FastMath.DEG_TO_RAD, 0)); 
    }
    
    public void elevate() {}
    public void depress() {}
    public void shoot() {}

    public String getName() { return name; }
    public Float getForwardThrust() { return forwardThrust; }
    public Float getTurnThrust() { return turnThrust; }
    
    public Float getOrderSpeed() { return orderSpeed; }
    public Float getOrderTurn() { return orderTurn; }
    public Float getOrderRoll() { return orderRoll; }
    public Float getOrderClimb() { return orderClimb; }
    
    public Float getOrderTarget() { return orderTarget; }
    public Node getTargeterNode() { return targeterNode; }
    
    public List<Battery> getBatteries() { return batteries; }

    
    public void setName(String name) { this.name = name; }
    public void setForwardThrust(Float forwardThrust) { this.forwardThrust = forwardThrust; }
    public void setTurnThrust(Float turnThrust) { this.turnThrust = turnThrust; }
    
    public void setOrderSpeed(Float orderSpeed) { this.orderSpeed = orderSpeed; }
    public void setOrderTurn(Float orderTurn) { this.orderTurn = orderTurn; }
    public void setOrderRoll(Float orderRoll) { this.orderRoll = orderRoll; }
    public void setOrderClimb(Float orderClimb) { this.orderClimb = orderClimb; }
    
    
    public void setOrderTarget(Float orderTarget) { this.orderTarget = orderTarget; }
    public void setTargeterNode(Node targeterNode) { this.targeterNode = targeterNode; }
    
    public void setBatteries(List<Battery> batteries) { this.batteries = batteries; }

}
